package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PStoreStaffBo;
import com.ruoyi.system.domain.vo.PStoreStaffVo;
import com.ruoyi.system.domain.PStoreStaff;
import com.ruoyi.system.mapper.PStoreStaffMapper;
import com.ruoyi.system.service.IPStoreStaffService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 门店店员Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-10
 */
@RequiredArgsConstructor
@Service
public class PStoreStaffServiceImpl implements IPStoreStaffService {

    private final PStoreStaffMapper baseMapper;

    /**
     * 查询门店店员
     */
    @Override
    public PStoreStaffVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询门店店员列表
     */
    @Override
    public TableDataInfo<PStoreStaffVo> queryPageList(PStoreStaffBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PStoreStaff> lqw = buildQueryWrapper(bo);
        Page<PStoreStaffVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询门店店员列表
     */
    @Override
    public List<PStoreStaffVo> queryList(PStoreStaffBo bo) {
        LambdaQueryWrapper<PStoreStaff> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PStoreStaff> buildQueryWrapper(PStoreStaffBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PStoreStaff> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUid() != null, PStoreStaff::getUid, bo.getUid());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), PStoreStaff::getAvatar, bo.getAvatar());
        lqw.eq(bo.getStoreId() != null, PStoreStaff::getStoreId, bo.getStoreId());
        lqw.like(StringUtils.isNotBlank(bo.getStaffName()), PStoreStaff::getStaffName, bo.getStaffName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), PStoreStaff::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getVerifyStatus()), PStoreStaff::getVerifyStatus, bo.getVerifyStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), PStoreStaff::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增门店店员
     */
    @Override
    public Boolean insertByBo(PStoreStaffBo bo) {
        PStoreStaff add = BeanUtil.toBean(bo, PStoreStaff.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改门店店员
     */
    @Override
    public Boolean updateByBo(PStoreStaffBo bo) {
        PStoreStaff update = BeanUtil.toBean(bo, PStoreStaff.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PStoreStaff entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除门店店员
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
